﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace PresentationLayer
{
    public partial class DelegateAuthority : System.Web.UI.Page
    {
        String userName = "COMM_Head";
        DelegateController delegateController = new DelegateController();
        int ActingManagerID;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                ActingManagerID = delegateController.getActingManagerID(userName);

                if (ActingManagerID != 0)
                {
                    //if there is data for ActingManagerID in department table
                    //get info of ActingManager
                    ActingManager am = delegateController.getActingManagerInfo(ActingManagerID);

                    //fill up all data
                    ddlEmployee.Items.Add(am.Employee.EmployeeName);
                    hiddenEmpID.Value = am.EmployeeID.ToString();
                    txtStartDate.Text = am.StartDate.ToString();
                    txtReason.Text = am.Reason;
                    btnDelegate.Text = "Revoke";
                    btnCancel.Visible = false;
                    txtReason.Enabled = false;
                    btnStartDate.Enabled = false;
                }

                else
                {
                    //if there is no data for ActingManagerID
                    ddlEmployee.DataSource = delegateController.getEmpList(userName);
                    ddlEmployee.DataTextField = "EmployeeName";
                    ddlEmployee.DataValueField = "EmployeeID";
                    ddlEmployee.DataBind();

                }
            }

        }

        protected void btnStartDate_Click(object sender, ImageClickEventArgs e)
        {
            Calendar1.Visible = true;
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            String sDate = Calendar1.SelectedDate.ToShortDateString();
            DateTime startDate = Convert.ToDateTime(sDate);
            if (startDate < DateTime.Today)
            {
                txtStartDate.Text = "Invalid";
            }
            else
            {
                txtStartDate.Text = sDate;
                Calendar1.Visible = false;
            }

        }

        protected void btnDelegate_Click(object sender, EventArgs e)
        {
            int delegationId;

            if (btnDelegate.Text == "Delegate")
            {
                //delegate the authority
                int empId = Convert.ToInt32(ddlEmployee.SelectedValue.ToString());
                DateTime startDate = Convert.ToDateTime(txtStartDate.Text);
                String reason = txtReason.Text;

                delegationId = delegateController.saveToActingManager(empId, startDate, reason);
                delegateController.sendNotificationtToDelegatedPerson(empId, userName, delegationId, "Delegate");
                Response.Redirect("~/DelegationSuccessful.aspx?status=Delegate");

            }
            else
            {
                //revoke the authority
                int empId = Convert.ToInt32(hiddenEmpID.Value.ToString());
                delegationId = delegateController.revokeAuthority(empId);
                delegateController.sendNotificationtToDelegatedPerson(empId, userName, delegationId, "Revoke");
                Response.Redirect("~/DelegationSuccessful.aspx?status=Revoke");
            }
        }
    }
}